package cn.zwxky.android.user.Login;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.net.MD5;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

import cn.zwxky.android.user.R;
import cn.zwxky.android.user.Utils.LittleUtils;
import cn.zwxky.android.user.Utils.NoDoubleClickUtils;
import cn.zwxky.android.user.Utils.URLString;

/**
 * 注册页面
 * Created by Administrator on 2017/3/15 0015.
 */
@ContentView(R.layout.aty_register)
public class RegisterActivity extends LsfbActivity {

    /**
     * 手机号码
     */
    @ViewInject(R.id.aty_register_et_phone)
    private EditText et_phone;

    /**
     * 验证码
     */
    @ViewInject(R.id.aty_register_et_code)
    private EditText et_code;

    /**
     * 密码
     */
    @ViewInject(R.id.aty_register_et_pwd1)
    private EditText et_pwd1;

    /**
     * 确认密码
     */
    @ViewInject(R.id.aty_register_et_pwd)
    private EditText et_pwd;

    /**
     * 获取验证码
     */
    @ViewInject(R.id.aty_register_text_code)
    private TextView text_code;

    /**
     * 计时器
     */
    private TimeCount time;

    @Override
    public void initView() {
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        LittleUtils.setsimpletitlebar(this, "注册");
        time = new TimeCount(60000, 1000);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_register_text_code, R.id.aty_register_btn_register})
    public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_phone.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(et_code.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(et_pwd1.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(et_pwd.getWindowToken(), 0);
        switch (view.getId()) {
            //获取验证码
            case R.id.aty_register_text_code:
                if (et_phone.getText().toString().isEmpty()){
                    SN.SnackShow(et_code,"请输入手机号码");
                    return;
                }
                SN.SnackShow(et_code,"正在获取验证码...");
                getCode();
                //计时器开始
                time.start();
                break;
            //注册
            case R.id.aty_register_btn_register:
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    register();
                }
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    public void register(){
        if (et_phone.getText().toString().isEmpty()){
            SN.SnackShow(et_code,"请输入手机号码");
            return;
        }
        if (et_pwd1.getText().toString().isEmpty()){
            SN.SnackShow(et_code,"请输入密码");
            return;
        }
        if (et_pwd.getText().toString().isEmpty()){
            SN.SnackShow(et_code,"请输入确认密码");
            return;
        }
        if (et_code.getText().toString().isEmpty()){
            SN.SnackShow(et_code,"请输入验证码");
            return;
        }

        if (!et_pwd.getText().toString().equals(et_pwd1.getText().toString())){
            SN.SnackShow(et_code,"两次输入密码不一致，请重新输入");
            et_pwd.setText("");
            et_pwd1.setText("");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("account",et_phone.getText().toString());
        hashMap.put("pwd", MD5.GetMD5Code(et_pwd1.getText().toString()));
        hashMap.put("code",et_code.getText().toString());

        new BaseInternet().getData(URLString.REGISTER, hashMap, RegisterBean.class, new RegisterEvent<RegisterBean>(), false);
    }

    @EventAnnotation
    public void RegisterEvent(RegisterEvent<RegisterBean> event){
        switch (event.getCode()){
            case 200:
                T.showShort(this,event.getMsg());
                startActivity(new Intent(this,LoginActivity.class));
                break;
            default:
                SN.SnackShow(et_code,event.getMsg());
                break;
        }
    }


    /**
     * 获取验证码
     */
    public void getCode(){

        HashMap<String, String> hashMap = new HashMap<String, String>();
          hashMap.put("account",et_phone.getText().toString());
        new BaseInternet().getData(URLString.REG_CODE, hashMap, GetCodeBean.class, new GetCodeEvent<GetCodeBean>(), false);
    }

    @EventAnnotation
    public void GetCodeEvent(GetCodeEvent<GetCodeBean> event){
        switch (event.getCode()){
            case 200:
                SN.SnackShow(et_code,"验证码获取成功，请注意稍后的短信信息");
                break;
            default:
                SN.SnackShow(et_code,event.getMsg());
                break;
        }
    }


    /**
     * 计时器
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        /**
         * 计时完毕
         */
        @Override
        public void onFinish() {// 计时完毕
            text_code.setText("获取验证码");
            text_code.setClickable(true);
            text_code.setTextColor(getResources().getColor(R.color.hint_color));
        }

        /**
         * 计时开始
         *
         * @param millisUntilFinished
         */
        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            text_code.setClickable(false);//防止重复点击
            text_code.setText(millisUntilFinished / 1000 + "秒后可重新获取");
            text_code.setTextColor(getResources().getColor(R.color.label_red));
        }
    }

}
